using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class Controls_Calendar : System.Web.UI.UserControl
{
    public string TargetControlID
    {
        get
        {
            return hTargetControlID.Value;
        }
        set
        {
            hTargetControlID.Value = value;
        }
    }
    public DateTime? DefaultDate
    {
        get
        {
            if (string.IsNullOrEmpty(hDefaultDate.Value))
                return null;
            else
                return DateTime.Parse(hDefaultDate.Value);
        }
        set
        {
            hDefaultDate.Value = value.HasValue ? value.Value.ToString() : string.Empty;
        }
    }
    public DateTime? MaxDate
    {
        get
        {
            if (string.IsNullOrEmpty(hMaxDate.Value))
                return null;
            else
                return DateTime.Parse(hMaxDate.Value);
        }
        set
        {
            hMaxDate.Value = value.HasValue ? value.Value.ToString() : string.Empty;
        }
    }
    public DateTime? MinDate
    {
        get
        {
            if (string.IsNullOrEmpty(hMinDate.Value))
                return null;
            else
                return DateTime.Parse(hMinDate.Value);
        }
        set
        {
            hMinDate.Value = value.HasValue ? value.Value.ToString() : string.Empty;
        }
    }

    protected void Page_PreRender(object sender, EventArgs e)
    {
        StringBuilder datePickerParams = new StringBuilder();
        datePickerParams.Append("showOn: 'none',");

        //Code for animation & speed (start)
        datePickerParams.Append("showAnim: 'fadeIn',speed: 'fast',");
        //Code for animation & speed (end)

        if (DefaultDate.HasValue)
            datePickerParams.AppendFormat("defaultDate: {0},", EncodeJsDate(DefaultDate.Value));
        if (MaxDate.HasValue)
            datePickerParams.AppendFormat("maxDate: {0},", EncodeJsDate(MaxDate.Value));
        if (MinDate.HasValue)
            datePickerParams.AppendFormat("minDate: {0},", EncodeJsDate(MinDate.Value));
        if (datePickerParams.Length > 0)
        {
            datePickerParams.Remove(datePickerParams.Length - 1, 1); //remove the trailing comma.
            datePickerParams.Insert(0, '{');
            datePickerParams.Append('}'); ;
        }

        TextBox targetControl = Parent.FindControl(TargetControlID) as TextBox;
        string clientId = targetControl.ClientID;

        imgCalendar.Attributes.Remove("onclick");
        if (datePickerParams.Length > 0)
            imgCalendar.Attributes.Add("onclick", "ShowCalParam('#" + clientId + "'," + datePickerParams.ToString() + ")");
        else
            imgCalendar.Attributes.Add("onclick", "ShowCal('#" + clientId + "')");
    }

    private string EncodeJsDate(DateTime date)
    {
        return "new Date('" + date.ToString("U") + " UTC" + "')";
    }
}
